<%
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
  xml.Document {

      style_header(xml,"<![CDATA[<font size='5'>Measurement Leq(1s): <b>$[decibel] dB(A)</b><br/>$[address]
</font>]]>")
    # Legend
    xml.Folder{
      xml.name("Legends")
      xml.open(0)
      xml.visibility(1)
      xml.name("Instruction")
      
      xml.visibility(1)
      xml.Folder{
        xml.name("static legends")
        xml.ScreenOverlay{
          xml.name("title")
          xml.Icon{
            xml.href("#{@baseurl}/images/legend.png")
          }
          xml.overlayXY(:x=>"0",:y=>"0.1", :xunits=>"fraction", :yunits=>"fraction")
          xml.screenXY(:x=>"0",:y=>"0.1", :xunits=>"fraction", :yunits=>"fraction")
        }
        xml.ScreenOverlay{
          xml.name("legend")
          xml.Icon{
            xml.href("#{@baseurl}/images/legend_info.png")
          }
          xml.overlayXY(:x=>"0",:y=>"1", :xunits=>"fraction", :yunits=>"fraction")
          xml.screenXY(:x=>"0",:y=>"1", :xunits=>"fraction", :yunits=>"fraction")
        }
      }
      xml.NetworkLink{
        xml.name("dynamic legends")
        xml.visibility(1)
        xml.open(0)
        xml.refreshVisibility(1)
        xml.flyToView(0)
        xml.Link{
          xml.href("#{@resource_url}.kml?dl=1")
          xml.refreshInterval(2)
          xml.viewRefreshMode("onStop")
          xml.viewRefreshTime(1)
        }
      }
    }

    # MEASURE LAYER
    xml.Folder{
      xml.name("Levels of exposure")
      xml.open(0)
      xml.visibility(1)
      @ranges.each_with_index { |range, i|
        xml.Folder{
          xml.name("#{range[:label]}")
          range[:measures].each_with_index { |point, i|
              generate_kml_measurement(xml,point)
            }
            #xml.Placemark{
             # xml.name("#{point.loudness} dB(A)")
              # #set the color according the loudness
             # xml.Style{
             #   xml.PolyStyle{
             #     color=get_color(point)
                  #  color="af"+point.color[1..6]
                  # #color=point.color[1..6]
             #     xml.color(color)
             #     xml.colorMode("normal")
#                  xml.fill(true)
#                  xml.outline(false)
#                }
#              }
#              geom=get_location(point)
#              coord=generate_kmlcircle(3100+point.loudness,geom.lng,geom.lat,10,6)
#
#              xml.MultiGeometry{
#                #    xml.Point{
#                #      xml.coordinates("#{point.lng},#{point.lat},#{point.loudness}")
#                #    }
#                xml.Polygon{
#                  xml.extrude(true)
#                  # #xml.tessellate(true) #xml.altitudeMode("clampToGround")
#                  xml.outerBoundaryIs{
#                    xml.LinearRing{
#                      xml.coordinates(coord)
#                    }
#                  }
#                }
#              }
#            }
#          }
        }
      }
    }


    # SEMANTIC LAYER
    xml.Folder{
      xml.name("Social Tagging layer")
      xml.open(1)
      xml.visibility(1)
      @tags.each { |tag|
        xml.Folder{
          xml.name("#{tag[:name]} (#{tag[:count]})")
          tag[:taggings].each { |tagging|
            # #p point.tags.size

            xml.Placemark{
              xml.name("#{tag[:name]} (#{tagging.tagger.login})")
              xml.description("<h4>Creator:</h4><br/> <img src=\"#{@baseurl}/users/#{tagging.tagger.id}.jpg\" width=\"50\" height=\"50\" />
                #<br/><a href=\"#{@baseurl}/users/#{tagging.tagger.id}\">#{tagging.tagger.login}</a>
                #<p><h4>Date:</h4>#{tagging.taggable.created_at}</p>
                #<p><h4>Annotation related measures - Leq(1sec) in dB(A)</h4>
                #<img src=\"#{@baseurl}/taggings/#{tagging.id}/measures.png\" width=\"250\" height=\"140\" />
                #</p>")
              #xml.extrude(true)
              #xml.altitudeMode("relativeToGround")
              
              geom=get_location(tagging.taggable)
              unless geom.nil?
                xml.Point{
                  xml.coordinates("#{geom.lng},#{geom.lat}")
                }
              end
            }

          }
        }
      }
    }

  }
}
%><%= xml.target! %>